package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.ArchivesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ArchivesForm;
import com.youlai.boot.system.model.query.ArchivesQuery;
import com.youlai.boot.system.model.vo.ArchivesVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 博客文章前端控制层
 *
 * @author youlaitech
 * @since 2025-03-15 19:25
 */
@Tag(name = "博客文章接口")
@RestController
@RequestMapping("/api/v1/archivess")
@RequiredArgsConstructor
public class ArchivesController  {

    private final ArchivesService archivesService;

    @Operation(summary = "博客文章分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:archives:query')")
    public PageResult<ArchivesVO> getArchivesPage(ArchivesQuery queryParams ) {
        IPage<ArchivesVO> result = archivesService.getArchivesPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增博客文章")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:archives:add')")
    public Result<Void> saveArchives(@RequestBody @Valid ArchivesForm formData ) {
        boolean result = archivesService.saveArchives(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取博客文章表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:archives:edit')")
    public Result<ArchivesForm> getArchivesForm(
            @Parameter(description = "博客文章ID") @PathVariable Long id
    ) {
        ArchivesForm formData = archivesService.getArchivesFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改博客文章")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:archives:edit')")
    public Result<Void> updateArchives(
            @Parameter(description = "博客文章ID") @PathVariable Long id,
            @RequestBody @Validated ArchivesForm formData
    ) {
        boolean result = archivesService.updateArchives(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除博客文章")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:archives:delete')")
    public Result<Void> deleteArchives(
            @Parameter(description = "博客文章ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = archivesService.deleteArchivess(ids);
        return Result.judge(result);
    }
}
